package com.allwinter.yibangsafe.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.allwinter.yibangsafe.domain.Comment;
import com.allwinter.yibangsafe.mapper.CommentMapper;
import com.allwinter.yibangsafe.mapper.LikePullMapper;
import com.allwinter.yibangsafe.service.CommentService;
import com.allwinter.yibangsafe.vo.LikePull;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class CommentServiceImpl implements CommentService {

    @Autowired(required = false)
    private CommentMapper commentMapper;

    @Autowired(required = false)
    private LikePullMapper likePullMapper;

    @Override
    public JSONObject getComment(Long ServiceId, Long userid) {
        List<Map<String,Object>> commentByServiceId = commentMapper.getCommentByServiceId(ServiceId);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userid",userid);
        List<LikePull> likepullList = likePullMapper.selectList(wrapper);
//        System.out.println(likepullList.toString());
        List<JSONObject> commentList = new ArrayList<>();
        for (int i = 0; i < commentByServiceId.size(); i++) {
            JSONObject comment = new JSONObject(commentByServiceId.get(i));
            for (int j = 0; j < likepullList.size(); j++) {
                if (comment.get("commentid").toString().equals(likepullList.get(j).getCommentid().toString())) {
                    comment.put("haslike","true");
                    break;
                } else {
                    comment.put("haslike","false");
                }
            }
            commentList.add(comment);
        }
        JSONObject res = new JSONObject();
        res.put("code",1);
        res.put("comments",commentList);
        res.put("comentNum",commentList.size());
        res.put("msg","get Comments success!");
        System.out.println(res.toString());
        return res;
    }

    @Override
    public JSONObject getComment(Long ServiceId) {
        JSONObject res = new JSONObject();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("serviceid",ServiceId);
        List<Comment> comments = commentMapper.selectList(wrapper);
        res.put("comments",comments);
        return res;
    }

    @Override
    public boolean delCommentByUserId(Long UserId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userid",UserId);
        int delete = commentMapper.delete(wrapper);
        if (delete == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean delCommentByCommentId(Long CommentId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("commentid",CommentId);
        int delete = commentMapper.delete(wrapper);
        if (delete == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean likeNumUp(Long Commentid) {
        Comment comment = commentMapper.selectById(Commentid);
        comment.setLikenum(comment.getLikenum()+1);
        int res = commentMapper.updateById(comment);
        if (res == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean addComment(String newComment,String userid,String serviceid) {
        JSONObject comment = JSONObject.parseObject(newComment);
        Comment savecomment = new Comment();
        savecomment.setUserid(Long.parseLong(userid));
        savecomment.setCommentid(Long.parseLong(comment.getString("id")));
        savecomment.setServiceid(Long.parseLong(serviceid));
        savecomment.setContent(comment.getString("content"));
        savecomment.setLikenum(Long.parseLong(comment.get("likeNum").toString()));
        if (comment.get("parentId") == null) {
            savecomment.setParentid(0l);
        } else {
            savecomment.setParentid(Long.parseLong(comment.get("parentId").toString()));
        }

        savecomment.setCreatetime(comment.getString("createTime"));
        int insert = commentMapper.insert(savecomment);
        if (insert == 1) {
            return true;
        }
        return false;
    }
}
